package controller;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import calendar.Person;

/**
 * activemq发送生产者
 * @author fqcd
 *
 * 2018年4月10日下午4:44:05
 */
@Controller
public class ActivemqController {
	
	private static final Gson gson = new GsonBuilder().create();
	
	@Resource
	private JmsTemplate jmsTemplate;
	@Resource
	private Destination topicDestination;
	@Resource
	private Destination queueDestination;
	
	/**
	 * 点对点
	 */
	@RequestMapping(value="sendqueue")
	public void sendqueue(){
		final Person person = new Person();
		person.setAge(12);
		person.setName("张学友");
	
		jmsTemplate.send(queueDestination, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				TextMessage textMessage = session.createTextMessage( gson.toJson(person));
				return textMessage;
			}
		});
		
	}
	
	
	
	/**
	 * 点对多
	 */
	@RequestMapping(value="sendtop")
	public void sendTopic(){
		jmsTemplate.send(topicDestination, new MessageCreator() {
			
			public Message createMessage(Session session) throws JMSException {
				TextMessage textMessage = session.createTextMessage( "这是sendtop");
				return textMessage;
			}
		});
		
	}

}
